<template>
    <div>
        <div style="padding: 1px 0">
          <el-input
            style="width: 200px; font-size:medium"
            prefix-icon="el-icon-search"
            placeholder="请输入用户名"
            v-model="username"
          ></el-input>
          <el-input
            style="width: 200px; font-size:medium"
            prefix-icon="el-icon-message"
            placeholder="请输入邮箱"
            class="ml-5"
            v-model="email"
          ></el-input>
          <el-button class="ml-5" type="primary" @click="load" style="font-size:medium" round>搜索</el-button>
          <el-button class="ml-5" type="info" @click="reset" style="font-size:medium" round>重置</el-button>
        </div>

        <div style="padding: 5px 0">
          <el-button class="el-icon-circle-plus-outline" type="primary" @click="handleAdd" style="font-size:medium">添加</el-button>
          <el-popconfirm
                class="ml-5"
                confirm-button-text='确定'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                title="您确定要批量删除吗?"
                @confirm="handleDeleteBatch">
          <el-button type="danger" slot="reference" style="font-size:medium">批量删除</el-button>
        </el-popconfirm>
          <el-button class="ml-5" type="warning" @click="exp" style="font-size:medium">导出</el-button>
        </div>

        <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg1'" @selection-change="handleSelectionChange" style="font-size:medium">
          <el-table-column type="selection" width="40"></el-table-column>
          <!-- <el-table-column
            prop="id"
            label="ID"
            width="70"
            align="center"
          ></el-table-column> -->
          <el-table-column
            prop="name"
            label="姓名"
            width="120"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="username"
            label="用户名"
            width="117"
            align="center"
          ></el-table-column>
          <el-table-column label="角色" width="127" align="center">
            <template v-slot="scope">
              <div v-if="scope.row.role === 'admin'">
                <el-tag type="warning" size="large"><span style="font-size:16px; font-weight:600">{{ scope.row.role }}</span></el-tag>
              </div>
              <div  v-else-if="scope.row.role === 'user'">
                <el-tag type="success" size="large"><span style="font-size:16px; font-weight:600">{{ scope.row.role }}</span></el-tag>
              </div>
              <div  v-else-if="scope.row.role === 'worker'">
                <el-tag type="danger" size="large"><span style="font-size:16px; font-weight:600">{{ scope.row.role }}</span></el-tag>
              </div>
              <div v-else>
                <el-tag type="warning" size="large"><span style="font-size:16px; font-weight:600">{{ scope.row.role }}</span></el-tag>
              </div>
           </template>
          </el-table-column>
          <el-table-column
            prop="phone"
            label="电话"
            width="195"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="email"
            label="邮箱"
            width="195"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="address"
            label="地址"
            width="140"
            align="center"
          ></el-table-column>
          <el-table-column label="操作" width="246" align="center">
            <template slot-scope="scope">
              <el-button type="success"
              @click="handleEdit(scope.row)"><span style="font-size:16px">编辑 <i class="el-icon-edit"></i></span></el-button>

              <el-popconfirm
                class="ml-5"
                confirm-button-text='确定删除'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                title="您确定要删除吗?"
                @confirm="handleDel(scope.row.id)"
                >
              <el-button type="danger" slot="reference"><span style="font-size:16px">删除 <i class="el-icon-remove-outline"></i></span></el-button>
            </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <div style="padding: 10px 0">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[7, 5, 10, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>

          <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="40%" style="margin-top: -60px;">
          <el-form label-width="80px" size="medium">
            <el-form-item label="姓名:" style="margin-left: 60px" size="large">
              <el-input clearable v-model="form.name" autocomplete="off" style="width: 70%; font-size:large"></el-input>
            </el-form-item>
            <el-form-item label="用户名:" style="margin-left: 60px" size="large">
              <el-input clearable v-model="form.username" autocomplete="off" style="width: 70%; font-size:large"></el-input>
            </el-form-item>
            <el-form-item label="角色:" style="margin-left: 60px;" size="large">
              <el-select v-model="form.role" placeholder="请选择角色" style="width: 70%;" size="large" >
                <el-option v-for="item in roles" :key="item.name" :label="item.name" :value="item.flag" style="font-size:large"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="电话:" style="margin-left: 60px" size="large">
              <el-input clearable v-model="form.phone" autocomplete="off" style="width: 70%; font-size:large"></el-input>
            </el-form-item>
            <el-form-item label="邮箱:" style="margin-left: 60px" size="large">
              <el-input clearable v-model="form.email" autocomplete="off" style="width: 70%; font-size:large"></el-input>
            </el-form-item>
            <el-form-item label="地址:" style="margin-left: 60px" size="large">
              <el-input clearable v-model="form.address" autocomplete="off" style="width: 70%; font-size:large"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer" style="margin-top:-30px">
            <el-button type="primary" @click="save" style="font-size:medium">确 定</el-button>
            <el-button type="warning" @click="cancel" style="font-size:medium">取 消</el-button>
          </div>
        </el-dialog>
        
    </div>
</template>

<script>
export default {
  name: 'User',
  data(){
    return{
        tableData: [],
        total: 0,
        pageNum: 1,
        pageSize: 7,
        username: "",
        email: "",
        address: "",
        dialogFormVisible: false,
        multipleSelection: [],
        form:{},
        isEdit: false,
        roles: []
    }
  },
  created(){
    this.load();
  },
  methods:{
    load() {
      this.request.get("/user/page", {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            username: this.username,
            email: this.email,
            address: this.address,
          },
        }).then((res) => {
          console.log(res);
          this.tableData = res.records;
          this.total = res.total;
        });

      this.request.get("/role").then( res =>{
        this.roles = res.data
      })
    },
    handleSizeChange(pageSize) {
      console.log(pageSize);
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum);
      this.pageNum = pageNum;
      this.load();
    },
    reset() {
      (this.username = ""), (this.email = ""), (this.address = ""), this.load();
    },
    handleAdd(){
      this.dialogFormVisible = true
      this.form = {}
    },
    handleEdit(row){
      console.log('row',row);
      this.form = Object.assign({},row)
      this.dialogFormVisible = true
      this.isEdit = true
    },
    handleDel(id){
      console.log('id',id);
      this.request.delete("/user/delete/"+id).then(res =>{
        if(res){
          this.$message.success('删除用户成功!')
          this.load()
        }else{
          this.$message.error('删除用户失败!')
          this.load()
        }
      })
    },
    handleDeleteBatch(){
      let ids = this.multipleSelection.map(v => v.id)
      console.log('ids',ids)
      if(ids.length === 0){
        this.$message.warning('批量删除用户前,请先勾选若干用户')
      }else{
        this.request.delete("/user/deleteBatch/",{data:ids}).then(res =>{
          if(res){
            this.$message.success('批量删除用户成功!')
            this.load()
          }else{
            this.$message.error('批量删除用户失败!')
            this.load()
          }
        })
      }
    },
    handleSelectionChange(val){
      this.multipleSelection = val
    },
    save(){
      if(this.isEdit === false){
        console.log('add');
        this.request.post("/user/save",this.form).then(res =>{
        console.log(res)
        if(res){
          console.log('西瓜');
          this.$message.success('添加用户成功!')
          this.dialogFormVisible = false
          this.load()
        }else{
          this.$message.error('添加用户失败!')
          this.dialogFormVisible = false
          this.load()
        }
      })
      }else{
        console.log('edit');
        this.request.put("/user/edit",this.form).then((res) =>{
          if(res){
            this.$message.success("修改用户信息成功!")
            this.dialogFormVisible = false
            this.isEdit = false
            this.load()
          }else{
            this.$message.error('修改用户信息失败!')
            this.dialogFormVisible = false
            this.isEdit = false
            this.load()
          }
        })
      }
    },
    cancel(){
      this.dialogFormVisible = false
      this.load()
    },
    exp(){
      window.open("http://8.138.58.18:7777/user/export")
    }
  }
};
</script>

<style>
.headerBg1 {
  background-color: antiquewhite !important;
}
</style>